package com.lm.service;

import com.lm.entity.Student;
import com.lm.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class StudentService implements IStudentService {
    @Autowired
    private StudentRepository studentRepository;

    @Override
    public Student getStudentByRollNum(Long rollNum) {
        Student obj = studentRepository.findById(rollNum).get();
        return obj;
    }

    @Override
    public Student getStudentByName(String name) {
        Student byName = studentRepository.findByName(name);
        return byName;
    }

    @Override
    public List<Student> getAllStudents() {
        List<Student> list = new ArrayList<>();
        studentRepository.findAll().forEach(e -> list.add(e));
        return list;
    }

    @Override
    public boolean addStudent(Student student) {
        studentRepository.save(student);
        return true;

    }

    @Override
    public void updateStudent(Student student) {
        studentRepository.save(student);
    }

    @Override
    public void deleteStudent(Long rollNum) {
        studentRepository.delete(getStudentByRollNum(rollNum));
    }
}

